package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.ExchangePacket;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.basic.AgentService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangePacketService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 换电套餐
 *
 * @Date:Created in 2018/7/27 11:48
 * @Author:
 * @Modified By: zxj 2018/7/27 11:48
 */
@Controller
@RequestMapping(value = "/security/hdw/exchange_packet")
public class ExchangePacketController extends SecurityController {

    @Autowired
    ExchangePacketService exchangePacketService;
    @Autowired
    AgentService agentService;

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_11.getValue());
    }
    /**
     * 换电套餐表格刷新
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.json.PageResult
     * @Author zxj 2018/7/27
     */
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(ExchangePacket search, HttpServletRequest request) {
        /*
        判断用户当前的角色（从sessionUser中获取）
            获取当前用户的ID
            代理商-运营商-总部
         */
        int type = getSessionUser(request).getType();
        // 代理商
        if (type == User.Type.AGENCY.getValue()) {
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
        // 运营商
        if (type == User.Type.AGENT.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        }
        return PageResult.successResult(exchangePacketService.findPage(search));
    }

    @RequestMapping(value = "add.htm")
    public void add(Model model) {
        model.addAttribute("types", ExchangePacket.Type.values());
    }
    /**
     * 新建
     * @Param [entity]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/7/28
     */
    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(ExchangePacket entity, HttpServletRequest request) {
        // 运营商新建代理商
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()) {
            entity.setAgentId(getSessionUser(request).getAgentId());
        }
        return exchangePacketService.create(entity);
    }

    @RequestMapping(value = "edit.htm")
    public String edit(Model model, Long id) {
        ExchangePacket entity = exchangePacketService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("types", ExchangePacket.Type.values());
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/exchange_packet/edit";
    }

    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(ExchangePacket entity) {
        return exchangePacketService.update(entity);
    }

    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(Long id) {
        return exchangePacketService.delete(id);
    }
    /**
     * 查看
     * @Param [model, id]
     * @Return java.lang.String
     * @Author zxj 2018/7/27
     */
    @RequestMapping(value = "view.htm")
    public String view(Model model, Long id) {
        ExchangePacket entity = exchangePacketService.find(id);
        // 业务重构
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        // 注入运营商名称
        if (entity.getAgentId() != null) {
            entity.setAgentName(agentService.find(entity.getAgentId()).getAgentName());
        }
        // 注入代理商名称（未做null判断）
        if (entity.getAgencyId() != null) {
            if (agentService.findAgencyName(entity.getAgencyId()) != null) {
                entity.setAgencyName(agentService.findAgencyName(entity.getAgencyId()).getAgencyName());
            }
        }
        model.addAttribute("entity", entity);

        model.addAttribute("id", id);
        return "/security/hdw/exchange_packet/view";
    }

}
